package com.thinkingstar.iads.datacenter.entity.dao;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.common.utils.StringUtil;
import com.thinkingstar.iads.datacenter.entity.DcEquipment;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignCell;
import com.thinkingstar.iads.datacenter.entity.DcTemplateVariable;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;

/**
 * @ClassName: DaoDcTemplateVariable
 * @Description:数据中心_模板_关联变量维护
 * @date 2018年6月19日
 */
@IocBean(args = {"refer:dao"})
public class DaoDcTemplateVariable extends EntityRepositoryServiceImpl<DcTemplateVariable>{

	public DaoDcTemplateVariable(Dao dao) {
		super(dao);
	}

	public List<DcTemplateVariable> getTemplateVariable(String template_id){
		List <DcTemplateVariable> list = null;
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false);
		criteria.where().and("template_id", "=", template_id);
		list = query(criteria);
		return list;
	}

	public Map<String,String> getVariableValues (List<DcTemplateVariable> variable,String project_id) {
		Map<String,String> map = new HashMap<>();
		List<NutMap> list = new ArrayList<>();
		for (int k = 0; k < variable.size(); k++) {
			DcTemplateVariable dcTemplateVariable = variable.get(k);
			if (dcTemplateVariable.getVariable_name().equals("PO_zhgqsbpbb")||dcTemplateVariable.getVariable_name().equals("PO_zhwxzxsbpbb")||dcTemplateVariable.getVariable_name().equals("PO_wzzksbpbb")){
				continue;
			}
			String find_sql = "";
			if (dcTemplateVariable.getBz().equals("SUM")){
				find_sql = "select sum("+dcTemplateVariable.getTable_field_name()+")as "+dcTemplateVariable.getTable_field_name()+" from "+dcTemplateVariable.getVariable_table_name() +" where project_id ='"+project_id+"' ";
				if (StringUtils.isNotEmpty(dcTemplateVariable.getConditions())){
					find_sql+= " and "+dcTemplateVariable.getConditions() ;
				}
			} else {
				find_sql = " select $field_name from $table_name ";
				if (!dcTemplateVariable.getVariable_table_name().toLowerCase().equals("dual")) {
					if (!dcTemplateVariable.getVariable_table_name().toLowerCase().equals("dc_project_mention_information")){
						if (!dcTemplateVariable.getVariable_table_name().toLowerCase().equals("dc_project_design_cell")){
							find_sql+=" where project_id = @project_id ";
							if (StringUtils.isNotEmpty(dcTemplateVariable.getConditions())){
								find_sql+=" and "+ dcTemplateVariable.getConditions();
							}
						} else {
							find_sql+=" where 1=1 ";
							if (StringUtils.isNotEmpty(dcTemplateVariable.getConditions())){
								find_sql+=" and "+ dcTemplateVariable.getConditions();
								find_sql+= " and del_flag='0' ";
							}
						}
					} else {
						find_sql+=" where 1=1 ";
						if (StringUtils.isNotEmpty(dcTemplateVariable.getConditions())){
							find_sql+=" and "+ dcTemplateVariable.getConditions();
							find_sql+= " and del_flag='0' ";
						}
					}
				}
			}

			Sql sql = Sqls.create(find_sql);
			sql.vars().set("table_name", dcTemplateVariable.getVariable_table_name());
			sql.vars().set("field_name", dcTemplateVariable.getTable_field_name());
			if (!dcTemplateVariable.getVariable_table_name().toLowerCase().equals("dual")) {
				sql.params().set("project_id", project_id);
			}
			sql.setCallback(new SqlCallback() {
				@Override
				public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
					List<NutMap> list = new ArrayList<>();
					while (rs.next()) {
						NutMap map = new NutMap();
						ResultSetMetaData metaData = rs.getMetaData();
						int count = metaData.getColumnCount();
						for (int i = count; i > 0; i--) {
							String filed = metaData.getColumnName(i);
							if (StringUtils.isNotEmpty(rs.getString(filed))){
								map.put(filed, rs.getString(filed));
							} else {
								map.put(filed, "");
							}

						}
						list.add(map);
					}
					return list;
				}
			});
			dao().execute(sql);
			list = sql.getList(NutMap.class);
			for (int i=0;i<list.size();i++) {
				map.put(dcTemplateVariable.getVariable_name()+"__"+dcTemplateVariable.getWord_type(),(String)list.get(i).get(dcTemplateVariable.getTable_field_name()));
			}
		}
		return map;
	}

	public QueryResult findTemplateVariablePageList(NutMap nutMap){
		List <DcTemplateVariable> page_list = null;
		Pager pager = null;
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false);
		if(nutMap.get("template_id") != null){
			criteria.where().and("template_id", "=", nutMap.get("template_id"));
		}
		if(nutMap.get("variable_table_name") != null){
			criteria.where().and("variable_table_name", "=", nutMap.get("variable_table_name"));
		}
		if(nutMap.get("variable_type") != null){
			criteria.where().and("variable_type", "=", nutMap.get("variable_type"));
		}
		if(nutMap.get("variable_name") != null){
			criteria.where().and("variable_name", "like", "%"+nutMap.get("variable_name")+"%");
		}

		criteria.getOrderBy().desc("create_time");
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null ){
			pager = new Pager(nutMap.getInt("page_num"),nutMap.getInt("page_size"));
			pager.setRecordCount(count(criteria));
			page_list = query(criteria, pager);
		}else{
			page_list = query(criteria);
		}
		return new QueryResult(page_list,pager);
	}


	public int updateTemplateVariable(DcTemplateVariable dcTemplateVariable){
		if(dcTemplateVariable == null){
			return 0;
		}
		int i = 0;
		String id = dcTemplateVariable.getId();
		if(StringUtils.isNotBlank(id)){
			DcTemplateVariable update_obj = fetch(id);
			update_obj.setUpdate_time(CommonDateUtil.getDateTime());
			update_obj.setConditions(dcTemplateVariable.getConditions());
			update_obj.setTable_field_name(dcTemplateVariable.getTable_field_name());
			update_obj.setVariable_name(dcTemplateVariable.getVariable_name());
			update_obj.setVariable_table_name(dcTemplateVariable.getVariable_table_name());
			update_obj.setVariable_type(dcTemplateVariable.getVariable_type());
			update_obj.setWord_type(dcTemplateVariable.getWord_type());
			update_obj.setDescription(dcTemplateVariable.getDescription());
			update_obj.setBz(dcTemplateVariable.getBz());
			i = update(update_obj);
		}else{
			dcTemplateVariable.setCreate_time(CommonDateUtil.getDateTime());
			Object object = insert(dcTemplateVariable);
			if(object != null){
				i = 1;
			}
		}
		return i;
	}
}
